package com.fitapp;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class HistoryWorkout extends Activity
{

	private DatabaseHelper helper;
	private SQLiteDatabase db;
	private String query;
	private Cursor cursor;
	@SuppressWarnings( "unused" )
	private String type;
	int id;
	double startTime;
	double endTime;
	int iterations;
	double duration;
	TextView dateField;
	TextView iterationsField;
	TextView durationField;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_history_workout2 );

		dateField = (TextView) findViewById( R.id.textView2 );
		iterationsField = (TextView) findViewById( R.id.textView4 );
		durationField = (TextView) findViewById( R.id.textView6 );

		Intent intent = getIntent( );
		if( intent.getExtras( ) != null )
		{
			showWorkout( intent.getIntExtra( "id", 1 ) );
		}
	}

	private void showWorkout( int intExtra )
	{
		helper = new DatabaseHelper( this );
		db = helper.getReadableDatabase( );
		query = new String( "SELECT * FROM Activities WHERE Id = " + intExtra );
		cursor = db.rawQuery( query, null );
		cursor.moveToFirst( );
		db.close( );
		helper.close( );

		id = Integer.parseInt( cursor.getString( 0 ) );
		type = cursor.getString( 1 );
		endTime = Integer.parseInt( cursor.getString( 2 ) );
		startTime = Integer.parseInt( cursor.getString( 3 ) );
		iterations = Integer.parseInt( cursor.getString( 4 ) );
		String it = iterations + " ";
		duration = endTime - startTime;
		String dur = duration + " Seconds";
		Calendar mydate = Calendar.getInstance( TimeZone.getTimeZone( TimeZone.getDefault( ).getID( ) ) );
		mydate.setTimeInMillis( (long) cursor.getInt( 2 ) * 1000 );
		String date = mydate.get( Calendar.DAY_OF_MONTH ) + "." + ( mydate.get( Calendar.MONTH ) + 1 ) + "." + " at " + mydate.get( Calendar.HOUR_OF_DAY ) + ":" + mydate.get( Calendar.MINUTE );
		;

		dateField.setText( date );
		iterationsField.setText( it );
		durationField.setText( dur );
	}
}
